{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ef31b610",
   "metadata": {},
   "source": [
    "----------  \n",
    "* 时间： 2022/11/12\n",
    "* 记录人：jkt  \n",
    "* 目标： 函数与模块\n",
    "----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff8a28af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 本周目标  \n",
    "> 1. 函数的定义  \n",
    "> 2. 如何使用函数  \n",
    "> 3.    \n",
    "> 4.  \n",
    "> 5.  \n",
    "> 6.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0d3112ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello\"+username+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4915159f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello杰瑞~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"杰瑞\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3100b765",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    \"\"\"y=2x的函数计算\"\"\"\n",
    "    print(2*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fe5b58f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "f(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d4bd6f09",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践，编程思维(计算思维)下，函数的应用\n",
    "def nfu_news_url(类别,page):\n",
    "    for page in range(page):\n",
    "        print(\"https://www.nfu.edu.cn/\"+类别+\"/index\"+str(page)+\".htm\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e56c430e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/tzgg/index0.htm\n",
      "https://www.nfu.edu.cn/tzgg/index1.htm\n",
      "https://www.nfu.edu.cn/tzgg/index2.htm\n",
      "https://www.nfu.edu.cn/tzgg/index3.htm\n",
      "https://www.nfu.edu.cn/tzgg/index4.htm\n",
      "https://www.nfu.edu.cn/tzgg/index5.htm\n",
      "https://www.nfu.edu.cn/tzgg/index6.htm\n",
      "https://www.nfu.edu.cn/tzgg/index7.htm\n",
      "https://www.nfu.edu.cn/tzgg/index8.htm\n",
      "https://www.nfu.edu.cn/tzgg/index9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"tzgg\",10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7ee0115",
   "metadata": {},
   "source": [
    "## 2.如何使用函数（传递实参）  \n",
    "  \n",
    "> 1. positional argument(位置参数)  \n",
    "> 2. keyword argument(关键字参数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e75c6f7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e1fd496c",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3059410298.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [28]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e9354805",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "839a5c4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3b10825c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b1536706",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "faac41b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "f(3,2,5) # 根据上面的经验，这个应该是位置参数吧（领悟）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a406b53a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "f(y=10,x=3,z=7) # 这个应该是关键字参数（领悟），关键字参数考虑顺序么？（不考虑顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "525e3ede",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9) # ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "395ef498",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=8) # ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "be5fd357",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3144590541.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [40]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(3,y=9,10) # ?\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(3,y=9,10) # ?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74d853e2",
   "metadata": {},
   "source": [
    "### 3. 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "86e3bb90",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "EOL while scanning string literal (1996180503.py, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [47]\u001b[1;36m\u001b[0m\n\u001b[1;33m    print(\"My\" + \"animal_type + \"'s name is\"+pet_name.title() + \".\")\u001b[0m\n\u001b[1;37m                                                                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pey_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a\" + animal_type + \".\")\n",
    "    print(\"My\" + \"animal_type + \"'s name is\"+pet_name.title() + \".\")\n",
    "    \n",
    "describe_pet('harry','hamster')\n",
    "describe_pet('python','cat')\n",
    "describe_pet('Jerry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "25bc7e1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world 你好 世界hello world 你好 世界\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",\"world\",\"你好\",\"世界\",end=\"\")\n",
    "print(\"hello\",\"world\",\"你好\",\"世界\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "bcd90e3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello||||world||||你好||||世界\n",
      "hello world 你好 世界\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",\"world\",\"你好\",\"世界\",sep='||||')\n",
    "print(\"hello\",\"world\",\"你好\",\"世界\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c4ae0924",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "41eee778",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'describe_pet' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [48]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m help(\u001b[43mdescribe_pet\u001b[49m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'describe_pet' is not defined"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bc04977",
   "metadata": {},
   "source": [
    "## 3.函数的返回值  \n",
    "> 1.几乎所有编程语言，函数是要求必须要 写 返回值！  \n",
    "> 2.Python作为高级语言，可以不 写 返回值，但是一般情况下，也是要写的，不写的话，默认返回None  \n",
    "> 3.函数的返回值：return  \n",
    ">> 3.1 return可以返回过程运算的结果  \n",
    ">> 3.2 return可以返回过程自定义结果(由程序开发人员/设计人员定制的）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25cfcc00",
   "metadata": {},
   "source": [
    "## 4.函数的优势  \n",
    "> 1.可重复使用  \n",
    "> 2.具有强大二点可拓展性  \n",
    "> 20具有可定制性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "dee83138",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y=x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "95bad671",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c7000f9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "0a33f91a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
